package thread;

import java.util.concurrent.ArrayBlockingQueue;

public class ABQDemo {
    private static ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(10);

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                abq.add(i);
            }
        });
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread t2 = new Thread(() -> {
            if (!abq.offer(11)) {
                System.out.println("insert 11 failure");
            }
            try {
                abq.put(11);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t2.start();
        Thread t3 = new Thread(() -> {
            Integer element;
            System.out.println("Start dequeue");
            while ((element = abq.poll()) != null) {
                System.out.print(" " + element);
            }
        });
        t3.start();
    }
}
